// 新建
var fs = require('fs')
var path = require('path')
var minify = require('html-minifier').minify


mkdir(path.resolve(__dirname, 'build', 'views'), function () {
    var dir = path.resolve(__dirname, 'src', 'views')
    var files = fs.readdirSync(dir);
    for (fn in files) {
        (function (fn) {
            var fname = files[fn]
            var fpath = dir + path.sep + fname;
            fs.lstatSync(fpath).isDirectory() && (function () {
                mkdir(path.resolve(__dirname, 'build', 'views', fname), function () {
                    var html = fs.readFileSync(
                        path.resolve(__dirname, 'src', 'views', fname, 'template.html'), {
                            encoding: 'utf8'
                        }
                    )

                    html = minify(html, {
                        removeComments: true,
                        collapseWhitespace: true,
                        removeAttributeQuotes: true
                    })

                    fs.writeFile(path.resolve(__dirname, 'build', 'views', fname, 'template.html'), html, 'utf8', (err) => {
                        if (err) throw err;
                        console.log('It\'s saved!');
                    });

                })
            })()
        })(fn)
    }
})

function mkdir(path, cb = function () { }) {
    fs.mkdir(path, 0777, function (err) {
        if (err) throw err
        cb() && console.log(path + " ... ... done")
    })
}

